package benlai_miniprogram;

import ocg.common.re.feed.Feed;
import ocg.common.re.feed.Feeder;
import ocg.common.re.file.ReString;
import ocg.common.re.jdbc.JdbcBuilder;
import ocg.common.re.jdbc.JdbcService;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * 传入值与数据库进行比对，
 */
public class SqlAssert{

    /**
     * 创建数据库链接对象
     */
    public static Connection connection ;

    /**
     * 是否开启数据库检查
     * @return
     */
    public static Connection sqlAssert(){
        if ( JdbcLink.isDB_Check ()) {
            return connection =
                    new JdbcBuilder (JdbcLink.getSql_server_url (),JdbcLink.getSql_server_user (), JdbcLink.getSql_server_pwd ())
                            .build();
        }
        return null;
    }

    /**
     * 更改数据库
     * @param sql
     * @return
     */
    public static boolean update( String sql ){

        if ( sql != null) {

            JdbcService service = new JdbcService( SqlAssert.connection );
            if ( service.update( sql )) {
                return true;
            }
        }
        return false;
    }

    /**
     * 数据库查询
     *
     * @param sql
     * @return
     */
    public static Feed feed( String sql ){
        if ( !ReString.isBlank ( sql )) {
            return Feeder.jdbcFeed ( SqlAssert.connection , sql );
        }

        return null;
    }

    public static void close(){
        if ( SqlAssert.connection != null) {
            try {
                SqlAssert.connection.close();

            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
